﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.Security;
using System.Web.Configuration;

namespace OnlineBusTicket
{
    public partial class BookTicket : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            Component.OnlineBusTicketEntities db = new Component.OnlineBusTicketEntities();
            MembershipUser u = Membership.GetUser(HttpContext.Current.User.Identity.Name);
            var obj = db.Members.Where(x => x.UserName == u.UserName).Take(1).FirstOrDefault();
            if (obj != null)
            {

                lblFullName.Text = obj.UserName;
                if (obj.Gender.ToString() == "True")
                {
                    lblGender.Text = "Male";
                }
                else
                {
                    lblGender.Text = "Female";
                }
                lblAge.Text = Convert.ToString(obj.Age);
                lblAddress.Text = obj.Address;
                lblPhone.Text = obj.Phone;
            }


            lblBusListID.Text = Search.BusListID;
            lblBusCatelogy.Text = Search.BusCatelogy;
            lblDeparturePlace.Text = Search.Departure;
            lblArrivePlace.Text = Search.Arrive;
            lblStartDate.Text = Convert.ToDateTime(Search.StartDate).ToShortDateString();
            lblTime.Text = Search.Time;
            decimal a=Convert.ToDecimal(Search.Price);
            if (ddlAge.SelectedIndex == 0)
            {
                lblPrice.Text = Convert.ToInt32(a).ToString();
            }
            if (ddlAge.SelectedIndex == 1)
            {
                a = a * 0;
                lblPrice.Text = Convert.ToInt32(a).ToString();
            }
            if (ddlAge.SelectedIndex == 2)
            {
                a = a / 2;
                lblPrice.Text = Convert.ToInt32(a).ToString();
            }
            if (ddlAge.SelectedIndex == 3)
            {
                lblPrice.Text = Convert.ToInt32(a).ToString();
            }
            if (ddlAge.SelectedIndex == 4)
            {
                a = a - a * 30 / 100;
                lblPrice.Text = Convert.ToInt32(a).ToString();
            }
            
        }

        protected void btnBook_Click(object sender, EventArgs e)
        {
            
                Component.OnlineBusTicketEntities db = new Component.OnlineBusTicketEntities();

                MembershipUser u = Membership.GetUser(HttpContext.Current.User.Identity.Name);
                var member = db.Members.SingleOrDefault(x => x.UserName == u.UserName);

                var CreateTicket = new Component.BusticKetDetail();
                CreateTicket.MemberId = member.MemberId;
                CreateTicket.BusListId = Search.BusListID;
                CreateTicket.BusCatelogy = Search.BusCatelogy;
                CreateTicket.FullName = txtFullName.Text;
                CreateTicket.Age = ddlAge.Text;
                CreateTicket.DeparturePlace = Search.Departure;
                CreateTicket.ArrivePlace = Search.Arrive;
                CreateTicket.StartDate = Convert.ToDateTime(Search.StartDate);
                CreateTicket.EndDate = Convert.ToDateTime(Search.EndDate);
                CreateTicket.Time = TimeSpan.Parse(Search.Time);
                CreateTicket.Price = Convert.ToDecimal(lblPrice.Text);
                CreateTicket.TicketStatus = "activated";
                db.BusticKetDetails.AddObject(CreateTicket);
                //db.SaveChanges();
                
                var totalseat = db.TotalSeats.SingleOrDefault(x => x.BusListId == lblBusListID.Text);
                totalseat.Seat = totalseat.Seat + 1;
                db.SaveChanges();
                Response.Redirect("~/Search.aspx");
                
            
        }

        protected void btnCancel_Click(object sender, EventArgs e)
        {
            Response.Redirect("Search.aspx");
        }
    }
}